package com.zuo.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;
import com.zuo.mall.entity.PmsSkuStock;
import com.zuo.mall.mapper.PmsSkuStockMapper;
import com.zuo.mall.service.PmsSkuStockService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsSkuStockServiceImpl extends ServiceImpl<PmsSkuStockMapper, PmsSkuStock> implements PmsSkuStockService {
    @Override
    public List<PmsSkuStock> getList(Long pid, String keyword) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId, pid);
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like(PmsSkuStock::getSkuCode, keyword);
        }
        List<PmsSkuStock> skuStockList = baseMapper.selectList(queryWrapper);

        return skuStockList;
    }

    @Override
    public int updateBatchByProductId(Long pid, List<PmsSkuStock> skuStockList) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId, pid);
        baseMapper.delete(queryWrapper);
        boolean b = this.saveBatch(skuStockList);
        if (b) return skuStockList.size();
        return 0;
    }
}
